package com.szsh.aiot.hsm.utils;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.utils.StringUtils;
import com.szsh.aiot.hsm.dto.enu.NumberEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Description:必要参数校验实体类
 * @Date 2023/8/25: 15:22
 * @Author: LOADING>>>>>
 */

@Component
public class PermitValidUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(PermitValidUtil.class);

    public static void main(String[] args) {
        String str =  "{\"operaType\":1,\"sequenceNo\":\"3fdea3075a2349988c3b269da7f7ecc2\",\"isOpen\":\"1\",\"tyAccount\":\"15366188252\",\"phone\":\"15366188252\",\"swithType\":\"all\",\"broadbandNO\":\"123456789\",\"timestamp\":1698828126807}";
        JsonObject jsonObject = new Gson().fromJson(str,JsonObject.class);
     //   System.out.println(jsonObject.get("sequenceNo"));
        System.out.println(getElementValue(jsonObject,"sequenceNo"));
    }
    /**
     * 从JSON中获取相应值
     * @param requestParams
     * @param memberName
     * @return
     */
    public static String getElementValue(JsonObject requestParams,String memberName){

        String value = null;
        AjaxResult  ajaxResult = parameterValid(requestParams,memberName);

        JsonElement jsonElement = null;
        requestParams.get(memberName);
        if (ajaxResult.get("code").equals(NumberEnum.NUM_INT_0)){
          value = (String) ajaxResult.get("msg");

        }
        return value;
    }

    /**
     * 必要参数校验
     * @param requestParams
     * @return
     */
    private static AjaxResult parameterValid(JsonObject requestParams,String memberName){

        if (!requestParams.has(memberName)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[{}}缺失],requestParams:[" + requestParams + "]",memberName);
            }
            return AjaxResult.badRequest(memberName+"缺失");
        }

        JsonElement jsonElement = requestParams.get(memberName);
        if (!jsonElement.isJsonPrimitive()) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[{}}格式错误],productId:[" + jsonElement + "]",memberName);
            }
            return AjaxResult.badRequest(memberName+"格式错误");
        }

       String membervalue = jsonElement.getAsString();
        if (StringUtils.isBlank(memberName)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[{}}为空],returnProject:[" + jsonElement + "]",memberName);
            }
            return AjaxResult.badRequest("{}}为空",memberName);
        }
        return AjaxResult.success(membervalue);
    }


}
